<?php

namespace App\Http\Controllers;

use App\Models\Admin;
use App\Models\AdminLog;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    /**
     * json返回格式
     * @param $data
     * @param string $message
     * @param int $code
     * @return \Illuminate\Http\JsonResponse
     */
    protected function apiResponse($data, $code = 0)
    {
        if ($code == 0) {
            return response()->json([
                'resultStatus' => $code == 0 ? true : false,
                'errorCode' => $code != 0 ? $code : null,
                'errorMessage' => null,
                'resultData' => $data

            ]);
        } else {
            return response()->json([
                'resultStatus' => false,
                'errorCode' => $code['errorCode'],
                'errorMessage' => $code['errorMessage'],
                'resultData' => null

            ]);
        }

    }

    function build_order_no($data = "")
    {
        return $data . date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

    public function getWxCode($path, $arr1)
    {
        $token = new \App\Services\Front\WxService();
        $access_token = $token->getAccessToken();
        $arr = [];
        foreach ($arr1 as $key => $val) {
            $arr[] = $key . "=" . urlencode($val);
        }
        $strPOST = join("&", $arr);
        $width = 430;
        $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token;
//    //二维码的宽度，默认为 430pX
        $data = array();
        $data["scene"] = $strPOST;    //
        $data["page"] = $path;
        $data["width"] = $width;
        $http = new \App\Libs\httpUtil();
        $res2 = $http->http_post($url, json_encode($data));
        $imgName = md5(date('ymdhis') . nonceStr(12)) . ".jpg";
        $file = env("WEB_BASE_ROOT") . $imgName;
        file_put_contents($file, $res2);
        $codeUrl = env("WEB_BASE_URL") . $imgName;
        return $codeUrl;
    }

    public function array_for_value($arr, $num)
    {
        $arr2 = [];
        for ($i = 0; $i < $num; $i++) {
            $arr2[$i] = $arr[$i];
        }
        unset($arr);
        return $arr2;
    }

    public function permissionAuthenticate($resource,$authenticate)
    {
        $resource = explode(';',$resource);
        if(in_array($authenticate,$resource)){
            return 1;
        }
        return 1;
    }

    public function addAdminLog($adminId, $userName, $url, $model, $title, $content, $ip)
    {
        $adminLog = [];
        $adminLog['adminId'] = $adminId;
        $adminLog['userName'] = $userName;
        $adminLog['url'] = $url;
        $adminLog['model'] = $model;
        $adminLog['title'] = $title;
        $adminLog['content'] = $content;
        $adminLog['ip'] = $ip;
        $adminLog['createTime'] = date("Y-m-d H:i:s");
        AdminLog::insert($adminLog);
    }

}
